package com.rongji.dfish.demo.editor.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.demo.constant.DemoConstants;
import com.rongji.dfish.demo.editor.service.EditorDirService;
import com.rongji.dfish.demo.entity.DfishView;
import com.rongji.dfish.demo.entity.DfishViewDir;
import com.rongji.dfish.demo.pub.view.PubView;
import com.rongji.dfish.framework.FrameworkHelper;
import com.rongji.dfish.ui.layout.ButtonBar;
import com.rongji.dfish.ui.layout.HorizontalLayout;
import com.rongji.dfish.ui.layout.VerticalLayout;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.widget.Button;
import com.rongji.dfish.ui.widget.Split;


public class EditorCommonView {

	public static final String MAIN_STATUS = "m_status";
	public static final String MAIN_TOOL = "m_tool";
	public static final String MAIN_CONTENT_SHELL = "m_content_shell";
	public static final String MAIN_CONTENT = "m_content";
	public static final String MAIN_REMARK = "m_remark";
	
	
	public static View buildMainView() {
		View view = PubView.buildBodyView();
		// 构建自己模块的布局
		view.replaceNodeById(buildMainLayout());
		return view;
	}
	
	public static VerticalLayout buildMainLayout() {
		
		VerticalLayout mRoot = PubView.buildMainLayout();
		
		HorizontalLayout mTop = (HorizontalLayout) mRoot.findNodeById(PubView.MAIN_TOP);
		mTop.setHeight("80");
		
		VerticalLayout mainTitleShell = (VerticalLayout) mRoot.findNodeById(PubView.MAIN_TITLE_SHELL);
		
		ButtonBar mTitle = new ButtonBar(PubView.MAIN_TITLE);
		mRoot.replaceNodeById(mTitle);
		
		HorizontalLayout mStatus = new HorizontalLayout(MAIN_STATUS);
		mainTitleShell.add(mStatus, "30");
		
		VerticalLayout mainOperShell = (VerticalLayout) mRoot.findNodeById(PubView.MAIN_OPER_SHELL);
		
		ButtonBar mTool = new ButtonBar(MAIN_TOOL).setStyle("margin-right:20px;").setWmin(10);
		mainOperShell.add(mTool, "30");
		
		VerticalLayout mDisplay = (VerticalLayout) mRoot.findNodeById(PubView.MAIN_DISPLAY);
		
		HorizontalLayout mContentShell = new HorizontalLayout(MAIN_CONTENT_SHELL);
		mDisplay.add(mContentShell);
		
		VerticalLayout mContent = new VerticalLayout(MAIN_CONTENT);
		mContentShell.add(mContent);
		mContent.setScroll(true).setStyle("margin:10px;").setWmin(20).setHmin(20);
		
		mContentShell.add(new Split().setCls("bd-split bd-onlyleft").setWmin(1), "1");
		
		VerticalLayout mRemark = new VerticalLayout(MAIN_REMARK);
		mContentShell.add(mRemark, "300");
		mRemark.setScroll(true).setCls("x-remark").setStyle("margin:10px;").setWmin(20).setHmin(20);
		
		return mRoot;
	}
	
	public static DfishViewDir getRootDir(String dirCreator) {
		DfishViewDir rootDir = new DfishViewDir();
		rootDir.setDirId(DemoConstants.ITEM_USER + dirCreator);
		rootDir.setDirName(getDirName(dirCreator));
		rootDir.setDirCreator(dirCreator);
		rootDir.setDirPath("");
		return rootDir;
	}
	
	public static String getDirName(String dirId) {
		String dirName = "";
		if (DemoConstants.USER_GUEST.equals(dirId)) {
			dirName = "游客大厅";
		} else if (DemoConstants.USER_ADMIN.equals(dirId)) {
			dirName = "模板案例"; // 管理登录时候显示模板案例,木有个人中心的目录
		} else {
			dirName = "个人中心";
		}
		return dirName;
	}
	
	public static class PathItem {
		String itemId;
		String itemName;
		String itemType;
		public String getItemId() {
			return itemId;
		}
		public void setItemId(String itemId) {
			this.itemId = itemId;
		}
		public String getItemName() {
			return itemName;
		}
		public void setItemName(String itemName) {
			this.itemName = itemName;
		}
		public String getItemType() {
			return itemType;
		}
		public void setItemType(String itemType) {
			this.itemType = itemType;
		}
	}
	
	public static List<PathItem> getDirItems(DfishViewDir dir) {
		if (dir == null) {
			return Collections.emptyList();
		}
		String dirPath = dir.getDirPath();
		String[] dirArray = null;
		int arrayLength = 0;
		if (dirPath == null) {
			dirPath = "";
		} else if (dirPath.startsWith("/")) {
			dirPath = dirPath.substring(1);
			dirArray = dirPath.split("/");
			arrayLength = dirArray.length;
		}
		List<PathItem> pathItems = new ArrayList<PathItem>(arrayLength+1);
		DfishViewDir rootDir = getRootDir(dir.getDirCreator());
		pathItems.add(parsePathItem(rootDir));
		
		EditorDirService editorDirService = FrameworkHelper.getBean(EditorDirService.class);
		Map<String, String> dirNames = editorDirService.findDirNamesBatch(dirArray);
		if (dirArray != null) {
			for (String dirId : dirArray) {
				PathItem item = new PathItem();
				item.setItemId(dirId);
				item.setItemName(dirNames.get(dirId));
				pathItems.add(item);
			}
		}
		return pathItems;
	}
	
	public static PathItem parsePathItem(DfishViewDir dir) {
		PathItem item = new PathItem();
		item.setItemId(dir.getDirId());
		item.setItemName(dir.getDirName());
		item.setItemType(DemoConstants.ITEM_DIR);
		return item;
	}
	
	public static PathItem parsePathItem(DfishView dView) {
		PathItem item = new PathItem();
		item.setItemId(dView.getViewId());
		item.setItemName(dView.getViewName());
		item.setItemType(DemoConstants.ITEM_FILE);
		return item;
	}
	
	public static void fillPathBar(ButtonBar btnBar, List<PathItem> pathItems) {
		if (Utils.isEmpty(pathItems)) {
			return;
		}
		btnBar.setSplit(new Split(">").setWidth(10));
		btnBar.setPub(new Button().setCls("f-button-link a-link").setOn(Button.EVENT_CLICK, "editor.dir.enter(this,$id.substring(2),$itemType);"));
		
		PathItem copyItem = new PathItem();
		boolean toUpper = pathItems.size() > 1;
		if (toUpper) {
			PathItem upperItem = pathItems.get(pathItems.size() - 2);
			Utils.copyPropertiesExact(copyItem, upperItem);
			
			copyItem.setItemName("返回上级");
			btnBar.add(buildPathButton(copyItem));
			btnBar.add(new Split("|").setWidth(10));
		}
		
		
		Button lastBtn = null;
		for (PathItem item : pathItems) {
			lastBtn = buildPathButton(item);
			btnBar.add(lastBtn);
		}
		// 最后一个按钮禁用
		lastBtn.setDisabled(true);
//			String targetPath = targetDir.getDirPath();
//			if (targetPath == null) {
//				targetPath = "";
//			}
//			String[] dirArray = targetPath.split("/");
//			
//			Set<String> dirIds = new HashSet<String>();
//			for (String dirId : dirArray) {
//				if (Utils.isEmpty(dirId)) {
//					continue;
//				}
//				dirIds.add(dirId);
//			}
//			EditorDirService editorDirService = FrameworkHelper.getBean(EditorDirService.class);
//			Map<String, DfishViewDir> dirMap = editorDirService.findDirsBatch(dirIds.toArray(new String[]{}));
		
		
//		int index = 1;
//		String dirCreator = targetDir.getDirCreator();
//		if (dirArray.length > 1) {
//			DfishViewDir upperDir = dirMap.get(dirArray[dirArray.length-2]);
//			if (upperDir == null) {
//				upperDir = new DfishViewDir();
//				upperDir.setDirId(DemoConstants.DIR_PRE_USER + dirCreator);
//			}
//			DfishViewDir copyDir = new DfishViewDir();
//			Utils.copyPropertiesExact(copyDir, upperDir);
//			copyDir.setDirName("返回上级");
//			
//			btnBar.add(buildPathBarButton(copyDir));
//			btnBar.add(new Split("|").setWidth(10));
//		}
//		for (String dirId : dirArray) {
//			DfishViewDir dir = null;
//			if (Utils.isEmpty(dirId)) {
//				dir = buildRootDir(dirCreator);
//			} else {
//				dir = dirMap.get(dirId);
//			}
//			if (dir == null) {
//				continue;
//			}
//			Button btn = buildPathBarButton(dir);
//			btnBar.add(btn);
//			// 显示路径的菜单
////				fillPathButton(btn, pathDirs, dirId, targetDir);
//			if (dirArray.length == index++) {
//				btn.setDisabled(true);
//			}
//		}
	}
	
	protected static Button buildPathButton(PathItem pathItem) {
		Button btn = new Button(pathItem.getItemName()).setId("b_"+pathItem.getItemId()).setData("itemType", pathItem.getItemType());
		return btn;
	}
	
//	public static void fillPathButton(Button btn, Map<String, List<DfishViewDir>> pathDirs, String pDirId, DfishViewDir targetDir) {
//		List<DfishViewDir> subDirs = pathDirs.get(pDirId);
//		if (Utils.notEmpty(subDirs)) {
//			for (DfishViewDir dir : subDirs) {
//				btn.add(buildPathBarButton(dir));
//			}
//		}
//	}
	
//	public static ButtonBar buildPathBar(Map<String, List<DfishViewDir>> pathDirs, DfishViewDir targetDir) {
//		ButtonBar btnBar = new ButtonBar(null);
//		fillPathBar(btnBar, pathDirs, targetDir);
//		return btnBar;
//	}

//	public static Button buildPathBarButton(DfishViewDir dir) {
//		Button btn = new Button(dir.getDirName()).setId("b_"+dir.getDirId());
//		return btn;
//	}
	
}
